# -*- coding: utf-8 -*-

def getlandmarks(str):
    """
    get face landmarks from json string.
    face++ api provide 83 landmarks
    """
    xlist = []
    ylist = []

    list = str.split("\"x\":") 
    for i in range(1, len(list)):
        tmplist = list[i].split(",")
        xstr = tmplist[0]
        x = int(xstr)
        xlist.append(x)

    list = str.split("\"y\":") 
    for i in range(1, len(list)):
        tmplist = list[i].split("}")
        ystr = tmplist[0]
        y = int(ystr)
        ylist.append(y)
    
    plist = []
    if len(xlist) == len(ylist):
        for i in range(len(xlist)):
            p = (xlist[i], ylist[i])
            plist.append(p)
    else:
        print("xlist len != ylist len, error")

    return plist


def transformLds83to68(lds83list):
    """
    change 83 face landmarks to 68 face landmarks
    src: face++ api 83 face landmarks
    dst: PFPLD 68 face landmarks
    """
    lds68list = [(-256, -256)] * 68

    # face chin boundary
    lds68list[0] = lds83list[1] 
    lds68list[1] = ((lds83list[2][0]+lds83list[3][0])//2, (lds83list[2][1]+lds83list[3][1])//2)
    lds68list[2] = lds83list[4] 
    lds68list[3] = lds83list[5] 
    lds68list[4] = lds83list[6] 
    lds68list[5] = lds83list[7] 
    lds68list[6] = lds83list[8] 
    lds68list[7] = lds83list[9] 

    lds68list[8] = lds83list[0] 

    lds68list[9] = lds83list[18] 
    lds68list[10] = lds83list[17] 
    lds68list[11] = lds83list[16] 
    lds68list[12] = lds83list[15] 
    lds68list[13] = lds83list[14] 
    lds68list[14] = lds83list[13] 
    lds68list[15] = ((lds83list[11][0]+lds83list[12][0])//2, (lds83list[11][1]+lds83list[12][1])//2)
    lds68list[16] = lds83list[10] 

    #left eyebrow
    lds68list[17] = lds83list[29] 
    lds68list[18] = ((lds83list[34][0]+lds83list[30][0])//2, (lds83list[34][1]+lds83list[30][1])//2)
    lds68list[19] = ((lds83list[35][0]+lds83list[31][0])//2, (lds83list[35][1]+lds83list[31][1])//2)
    lds68list[20] = ((lds83list[36][0]+lds83list[32][0])//2, (lds83list[36][1]+lds83list[32][1])//2)
    lds68list[21] = lds83list[33] 

    #right eyebrow
    lds68list[22] = lds83list[75] 
    lds68list[23] = ((lds83list[80][0]+lds83list[76][0])//2, (lds83list[80][1]+lds83list[76][1])//2)
    lds68list[24] = ((lds83list[81][0]+lds83list[77][0])//2, (lds83list[81][1]+lds83list[77][1])//2)
    lds68list[25] = ((lds83list[82][0]+lds83list[78][0])//2, (lds83list[82][1]+lds83list[78][1])//2)
    lds68list[26] = lds83list[79] 

    #nose
    lds68list[27] = ((lds83list[55][0]+lds83list[59][0])//2, (lds83list[55][1]+lds83list[59][1])//2)
    lds68list[28] = (int(lds68list[27][0]*0.7+lds83list[64][0]*0.3), int(lds68list[27][1]*0.7+lds83list[64][1]*0.3))
    lds68list[29] = (int(lds68list[27][0]*0.3+lds83list[64][0]*0.7), int(lds68list[27][1]*0.3+lds83list[64][1]*0.7))
    lds68list[30] = lds83list[64]
    lds68list[31] = ((lds83list[57][0]+lds83list[62][0])//2, (lds83list[57][1]+lds83list[62][1])//2)
    lds68list[32] = lds83list[57]
    lds68list[33] = lds83list[58]
    lds68list[34] = lds83list[61]
    lds68list[35] = ((lds83list[61][0]+lds83list[63][0])//2, (lds83list[61][1]+lds83list[63][1])//2)
   
    #left eye
    lds68list[36] = lds83list[21]
    lds68list[37] = ((lds83list[26][0]+lds83list[27][0])//2, (lds83list[26][1]+lds83list[27][1])//2)
    lds68list[38] = ((lds83list[26][0]+lds83list[28][0])//2, (lds83list[26][1]+lds83list[28][1])//2)
    lds68list[39] = lds83list[25]
    lds68list[40] = ((lds83list[19][0]+lds83list[23][0])//2, (lds83list[19][1]+lds83list[23][1])//2)
    lds68list[41] = ((lds83list[19][0]+lds83list[22][0])//2, (lds83list[19][1]+lds83list[22][1])//2)

    #right eye
    lds68list[42] = lds83list[67]
    lds68list[43] = ((lds83list[72][0]+lds83list[73][0])//2, (lds83list[72][1]+lds83list[73][1])//2)
    lds68list[44] = ((lds83list[72][0]+lds83list[74][0])//2, (lds83list[72][1]+lds83list[74][1])//2)
    lds68list[45] = lds83list[71]
    lds68list[46] = ((lds83list[65][0]+lds83list[69][0])//2, (lds83list[65][1]+lds83list[69][1])//2)
    lds68list[47] = ((lds83list[65][0]+lds83list[68][0])//2, (lds83list[65][1]+lds83list[68][1])//2)

    #outer-mouth
    lds68list[48] = lds83list[37]
    lds68list[49] = lds83list[49]
    lds68list[50] = lds83list[48]
    lds68list[51] = lds83list[54]
    lds68list[52] = lds83list[51]
    lds68list[53] = lds83list[52]
    lds68list[54] = lds83list[46]
    lds68list[55] = lds83list[43]
    lds68list[56] = lds83list[44]
    lds68list[57] = lds83list[38]
    lds68list[58] = lds83list[41]
    lds68list[59] = lds83list[40]

    #inner-mouth
    lds68list[60] = ((lds83list[40][0]+lds83list[49][0])//2, (lds83list[40][1]+lds83list[49][1])//2)
    lds68list[61] = lds83list[50]
    lds68list[62] = lds83list[47]
    lds68list[63] = lds83list[53]

    lds68list[64] = ((lds83list[43][0]+lds83list[52][0])//2, (lds83list[43][1]+lds83list[52][1])//2)
    lds68list[65] = lds83list[42]
    lds68list[66] = lds83list[45]
    lds68list[67] = lds83list[39]

    return lds68list


def transformLds68to98(lds68list):
    """
    change 68 face landmarks to 98 face landmarks
    src: PFPLD 68 face landmarks
    dst: PFPLD 98 face landmarks
    """
    lds98list = [(-256, -256)] * 98
   
    # face chin boundary, 33 lds
    for i in range(17):
        lds98list[i*2] = lds68list[i]

    #left eyebrow, 9 lds. 33+9=42. 17+5=22
    for i in range(5):
        lds98list[33+i] = lds68list[17+i] 

    #right eyebrow, 9 lds. 42+9=51. 22+5=27
    for i in range(5):
        lds98list[42+i] = lds68list[22+i] 

    #nose, 9 lds. 51+9=60. 27+9=36
    for i in range(9):
        lds98list[51+i] = lds68list[27+i] 

    #left eye, 8 lds. 60+8=68. 36+6=42
    lds98list[60] = lds68list[36] 
    lds98list[61] = lds68list[37] 
    
    lds98list[63] = lds68list[38] 
    lds98list[64] = lds68list[39] 
    lds98list[65] = lds68list[40] 

    lds98list[67] = lds68list[41] 

    #right eye, 8 lds. 68+8=76. 42+6=48
    lds98list[68] = lds68list[42] 
    lds98list[69] = lds68list[43] 
    
    lds98list[71] = lds68list[44] 
    lds98list[72] = lds68list[45] 
    lds98list[73] = lds68list[46] 

    lds98list[75] = lds68list[47] 

    #outer-mouth, 12 lds. 76+12=88, 48+12=60
    for i in range(12):
        lds98list[76+i] = lds68list[48+i]

    #inner-mouth, 8 lds. 88+10=98, 60+8=68
    for i in range(8):
        lds98list[88+i] = lds68list[60+i]

    return lds98list

def modifyEyeLds(lds98list, modifiedLds):
    """
    modify eye landmarks
    """
    if len(modifiedLds) == 0:
        return

    if len(modifiedLds) == 6:
        modifyCenterX = (modifiedLds[0][0] + modifiedLds[3][0]) / 2
        leftCenterX = (lds98list[60][0] + lds98list[64][0]) / 2
        rightCenterX = (lds98list[68][0] + lds98list[72][0]) / 2
        if abs(modifyCenterX - leftCenterX) < abs(modifyCenterX - rightCenterX):
            # modify left eye lds
            lds98list[60] = modifiedLds[0] 
            lds98list[61] = modifiedLds[1] 
            lds98list[63] = modifiedLds[2] 
            lds98list[64] = modifiedLds[3] 
            lds98list[65] = modifiedLds[4] 
            lds98list[67] = modifiedLds[5] 
        else:
            # modify right eye lds
            lds98list[68] = modifiedLds[0] 
            lds98list[69] = modifiedLds[1] 
            lds98list[71] = modifiedLds[2] 
            lds98list[72] = modifiedLds[3] 
            lds98list[73] = modifiedLds[4] 
            lds98list[75] = modifiedLds[5] 

    if len(modifiedLds) == 12:
        # modify left eye lds
        lds98list[60] = modifiedLds[0] 
        lds98list[61] = modifiedLds[1] 
        lds98list[63] = modifiedLds[2] 
        lds98list[64] = modifiedLds[3] 
        lds98list[65] = modifiedLds[4] 
        lds98list[67] = modifiedLds[5] 
        # modify right eye lds
        lds98list[68] = modifiedLds[6] 
        lds98list[69] = modifiedLds[7] 
        lds98list[71] = modifiedLds[8] 
        lds98list[72] = modifiedLds[9] 
        lds98list[73] = modifiedLds[10] 
        lds98list[75] = modifiedLds[11] 
            

if __name__ == "__main__":
    print("hahah")
